package com.quanyan.club.task.tracker;

import com.alibaba.fastjson.JSONObject;
import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.tasktracker.Result;
import com.github.ltsopensource.tasktracker.runner.JobContext;
import com.github.ltsopensource.tasktracker.runner.JobRunner;
import com.quanyan.api.APIResponse;
import com.quanyan.club.entity.db.*;
import com.quanyan.club.mapper.ClubActivityMapper;
import com.quanyan.club.mapper.TbClubActivityMapper;
import com.quanyan.club.mapper.TbClubMapper;
import com.quanyan.club.mapper.TbSignUserMapper;
import com.quanyan.club.service.ClubActivityService;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.exception.BizException;
import com.quanyan.redisCluster.config.RedisTemplate;
import com.quanyan.redisCluster.utils.RedisConstants;
import com.quanyan.redisCluster.utils.RedisKey;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * Created by yue.yuan on 2016/11/3.
 */
@Component
public class ActivitySettlementJob implements JobRunner {

    private static Logger logger = Logger.getLogger(ActivitySettlementJob.class);

    @Autowired
    private ClubActivityService clubActivityService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ClubActivityMapper clubActivityMapper;

    @Autowired
    private TbSignUserMapper tbSignUserMapper;

    @Autowired
    private TbClubActivityMapper tbClubActivityMapper;
    @Autowired
    private TbClubMapper tbClubMapper;

    @Override
    public Result run(JobContext jobContext) throws Throwable {
        logger.warn("开始活动自动结算");

        Job job = jobContext.getJob();
        Map<String, String> params = job.getExtParams();
        logger.warn("活动自动结算任务，参数：" + params);

        //1. 获取活动信息，俱乐部信息
        int activityId = Integer.valueOf(params.get("activityId"));
        int clubId = Integer.valueOf(params.get("clubId"));
        TbClub club = tbClubMapper.selectByPrimaryKey(clubId);
        Integer userId = club.getUid();
        if(null == userId || userId == 0)
        {
            logger.warn("这个活动的创建者不见了！");
            return new Result(Action.EXECUTE_SUCCESS);
        }
        clubActivityService.activitySettlementAuto(activityId, clubId, userId);
        return new Result(Action.EXECUTE_SUCCESS);
    }
}
